/**
 * @file main.cpp
 * @brief VimL虚拟机命令行界面
 * @author VimLVM Team
 * @date 2025
 */

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <iomanip>
#include "viml/vm.h"
#include "viml/value.h"
#include "viml/instruction.h"
#include "viml/compiler.h"

using namespace viml;

void print_usage(const char* program_name) {
    std::cout << "VimL Virtual Machine v1.0.0" << std::endl;
    std::cout << "Usage: " << program_name << " [options] <file>" << std::endl;
    std::cout << "Options:" << std::endl;
    std::cout << "  -h, --help    Show this help message" << std::endl;
    std::cout << "  -v, --version Show version information" << std::endl;
    std::cout << std::endl;
    std::cout << "Examples:" << std::endl;
    std::cout << "  " << program_name << " simple_test.viml" << std::endl;
}

void print_version() {
    std::cout << "VimL Virtual Machine v1.0.0" << std::endl;
    std::cout << "Phase 2: Garbage Collection & Runtime Optimizations" << std::endl;
}

std::string read_file(const std::string& filename) {
    std::ifstream file(filename);
    if (!file.is_open()) {
        throw std::runtime_error("Cannot open file: " + filename);
    }
    
    std::stringstream buffer;
    buffer << file.rdbuf();
    return buffer.str();
}



// 打印字节码指令
void print_bytecode(const std::shared_ptr<Function>& func) {
    std::cout << "\n=== Bytecode Dump ===" << std::endl;
    std::cout << "Function: " << func->name << std::endl;
    std::cout << "Instructions: " << func->bytecode.size() << std::endl;
    
    for (size_t i = 0; i < func->bytecode.size(); ++i) {
        const auto& inst = func->bytecode[i];
        std::cout << std::setw(3) << i << ": " << inst.to_string() << std::endl;
    }
    std::cout << "==================" << std::endl;
}

// 编译VimL源代码到字节码 - 使用专门的编译器
std::shared_ptr<Function> compile_viml(const std::string& source, const std::string& filename) {
    viml::Compiler compiler;
    
    std::cout << "DEBUG: Source file content:" << std::endl;
    std::cout << source << std::endl;
    std::cout << "DEBUG: Source file end" << std::endl;
    
    auto result = compiler.compile(source, filename);
    
    if (!result.success) {
        for (const auto& error : result.errors) {
            std::cerr << "Compilation error: " << error << std::endl;
        }
        return nullptr;
    }
    
    // 打印字节码
    print_bytecode(result.function);
    
    return result.function;
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        print_usage(argv[0]);
        return 1;
    }
    
    std::string arg = argv[1];
    
    if (arg == "-h" || arg == "--help") {
        print_usage(argv[0]);
        return 0;
    }
    
    if (arg == "-v" || arg == "--version") {
        print_version();
        return 0;
    }
    
    try {
        // 读取VimL源文件
        std::string source = read_file(argv[1]);
        
        std::cout << "Compiling and running: " << argv[1] << "\n";
        std::cout << "DEBUG: File read successfully, size: " << source.size() << " bytes" << std::endl;
        
        // 编译VimL源代码
        auto func = compile_viml(source, argv[1]);
        
        // 执行程序
        VM vm;
        ErrorCode result = vm.execute(func);
        
        if (result == ErrorCode::SUCCESS) {
            std::cout << "Program executed successfully" << std::endl;
        } else {
            std::cerr << "Execution failed: " << vm.last_error() << std::endl;
            return static_cast<int>(result);
        }
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << "\n";
        return 1;
    }
    
    return 0;
}